import copy
from rest_framework.views import APIView
from SearchEngines.Function import getUserAnswer
from SecureHelperServer.settings import formatResponseData
from utils.conf import Response


class AIChat(APIView):
    # 局部(本视图有效)认证类
    authentication_classes = []
    # 局部(本视图有效)权限类
    permission_classes = []

    def post(self, request, *args, **kwargs):
        """
        快问快答
        """
        data = copy.deepcopy(formatResponseData)
        try:
            msg = request.data.get("msg")

            if not msg:
                return Response(data=data, responseMsg="请求参数缺失", loggerMsg="请求参数缺失", isError=True)

            answer = getUserAnswer(msg=msg)
            data["data"]["answer"] = answer
            print(answer)
            return Response(data=data, loggerMsg=f"快问快答：AI回复成功，msg={msg}")

        except Exception as e:
            return Response(data=data, responseMsg="应答失败",
                            loggerMsg=f"请求处理失败: {e}", isError=True)
